/**
 * @file 日期工具
 */
class DateTimeUtil {
    private TAG: string = 'DateTimeUtil'

    /**
     * 格式化当前时间
     */
    formatNowTime(): number[] {
        let date = new Date()
        return [date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()]
    }

    /**
     * 是否整点时间
     */
    isWholeHour(): boolean {
        let date = new Date()
        return date.getMinutes() == 0 && date.getSeconds() == 0
    }

    getSecond(time: number): string {
        let date = new Date()
        date.setTime(time)
        return date.getSeconds().toString()
    }

    /**
     * 获取分钟的第一毫秒
     * @param time
     */
    getFirstMilliseconds(time: number): number {
        let date = new Date()
        date.setTime(time)
        date.setSeconds(0, 0)
        return date.getTime()
    }

    /**
     * 获取分钟的最后一毫秒
     * @param time
     */
    getLastMilliseconds(time: number): number {
        let date = new Date()
        date.setTime(time)
        date.setSeconds(59, 999)
        return date.getTime()
    }

    /**
     * 获取之前某一天日期对象
     * @param days 日期对象
     */
    getBeforeDate(days: number): Date {
        let d = new Date()
        d.setFullYear(d.getFullYear(), d.getMonth(), d.getDate() - days)
        d.setHours(0, 0, 0, 0)
        return d
    }

    /**
     * 获取上一分钟
     */
    getBeforeTime(): Date {
        let d = new Date()
        d.setMinutes(d.getMinutes() - 1, 0, 0)
        return d
    }

    getTodayStart(): number {
        let d = new Date()
        d.setFullYear(d.getFullYear(), d.getMonth(), d.getDate())
        d.setHours(0, 0, 0, 0)
        return d.getTime()
    }

    getTodayEnd(): number {
        let d = new Date()
        d.setFullYear(d.getFullYear(), d.getMonth(), d.getDate())
        d.setHours(23, 59, 59, 0)
        return d.getTime()
    }

    getCurrentHourStart(): number {
        let d = new Date()
        d.setFullYear(d.getFullYear(), d.getMonth(), d.getDate())
        d.setHours(d.getHours(), 0, 0, 0)
        return d.getTime()
    }

    getCurrentHourEnd(): number {
        let d = new Date()
        d.setFullYear(d.getFullYear(), d.getMonth(), d.getDate())
        d.setHours(d.getHours(), 59, 59, 0)
        return d.getTime()
    }

    /**
     * 时分秒
     */
    getTime() {
        const DATETIME = new Date()
        return this.concatTime(DATETIME.getHours(), DATETIME.getMinutes())
    }

    getTimeWithSecond() {
        const DATETIME = new Date()
        return this.concatTimeWithSecond(DATETIME.getHours(), DATETIME.getMinutes(), DATETIME.getSeconds())
    }

    getTimeByTime(dateTime) {
        const DATETIME = new Date()
        DATETIME.setTime(dateTime)
        return this.concatTimeWithSecond(DATETIME.getHours(), DATETIME.getMinutes(), DATETIME.getSeconds())
    }

    /**
     * 年月日
     */
    getDate() {
        const DATETIME = new Date()
        return this.concatDate(DATETIME.getFullYear(), DATETIME.getMonth() + 1, DATETIME.getDate())
    }

    getWeek() {
        const DATETIME = new Date()
        const arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        return arr[DATETIME.getDay()]
    }

    /**
     * 日期不足两位补充0
     * @param value-数据值
     */
    fill(value: number) {
        return (value > 9 ? '' : '0') + value
    }

    /**
     * 年月日格式修饰
     * @param year
     * @param month
     * @param date
     */
    concatDate(year: number, month: number, date: number) {
        return `${year}-${this.fill(month)}-${this.fill(date)}`
    }

    /**
     * 时分秒格式修饰
     * @param hours
     * @param minutes
     */
    concatTime(hours: number, minutes: number) {
        return `${this.fill(hours)}:${this.fill(minutes)}`
    }

    concatTimeWithSecond(hours: number, minutes: number, seconds: number) {
        return `${this.fill(hours)}:${this.fill(minutes)}:${this.fill(seconds)}`
    }

    /**
     * 通过时间戳获取日期
     * @param date
     */
    getResDate(date) {
        let myDate = new Date(date * 1000)
        return this.concatDate(myDate.getFullYear(), myDate.getMonth() + 1, myDate.getDate())
    }

    /**
     * 获取历史数据时间段
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    getHistoryDate(startTime, endTime) {
        let date = new Date()
        date.setTime(startTime)
        let endDate = new Date()
        endDate.setTime(endTime)
        return `${this.concatDate(date.getFullYear(), date.getMonth() + 1, date.getDate())} ${this.concatTime(date.getHours(), date.getMinutes())} - ${this.concatTime(endDate.getHours(), endDate.getMinutes())}`
    }
    /**
     * 获取月日
     */
    getMonthDate(date) {
        let myDate = new Date(date * 1000)
        return `${myDate.getMonth() + 1}月${myDate.getDate()}日`
    }
    /**
     * 通过毫秒转换，展示视频/音频时长
     * @param time
     */
    getPlayTime(time) {
        let min = Math.floor((time / 1000) / 60)
        let seconds = Math.floor((time / 1000) % 60)
        return `${this.fill(min)}:${this.fill(seconds)}`
    }
}

export default new DateTimeUtil()